package com.lly.songhigh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lly.songhigh.R;
import com.lly.songhigh.application.BaseApplication;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.Map;
import java.util.Set;

/**
 * Created by 遇博 on 15/10/30.
 */
public class LoadingActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout qq_layout, sina_layout;
    private UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.login");
    private Toolbar loginToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jump_loading_activity);
        BaseApplication.getInstance().addActivity(this);
        initView();
    }

    private void initView() {
        qq_layout = (LinearLayout) findViewById(R.id.qq_layout);
        sina_layout = (LinearLayout) findViewById(R.id.sina_layout);
        loginToolbar = (Toolbar) findViewById(R.id.login_toolbar);
        loginToolbar.setNavigationIcon(R.mipmap.toolbar_back);
        loginToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        qq_layout.setOnClickListener(this);
        sina_layout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qq_layout:
                UMQQSsoHandler qqSsHandler = new UMQQSsoHandler(LoadingActivity.this, "100424468", "c7394704798a158208a74ab60104f0ba");
                qqSsHandler.addToSocialSDK();
                mController.doOauthVerify(LoadingActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        Toast.makeText(LoadingActivity.this, "授权开始", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete(Bundle bundle, SHARE_MEDIA share_media) {
                        Toast.makeText(LoadingActivity.this, "授权成功", Toast.LENGTH_LONG).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(LoadingActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(LoadingActivity.this, "获取数据平台开始", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                if (status == 200 && info != null) {
                                    StringBuilder sb = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys) {
                                        sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    Log.d("TestData", sb.toString());
                                    //获取用户名
                                    String textuer = (String) info.get("screen_name");
                                    //获取头像
                                    String url = info.get("profile_image_url").toString();
                                    Intent intent = new Intent();
                                    intent.putExtra("profile_image_url", url);
                                    intent.putExtra("screen_name", textuer);
                                    setResult(100, intent);
                                    finish();
                                } else {
                                    Log.d("TestData", "发生错误" + status);
                                }

                            }
                        });
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA share_media) {
                        Toast.makeText(LoadingActivity.this, "授权错误", Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        Toast.makeText(LoadingActivity.this, "授权取消", Toast.LENGTH_SHORT).show();

                    }
                });

                break;
            case R.id.sina_layout:
                //第三方新浪微博登录
                mController.getConfig().setSsoHandler(new SinaSsoHandler());
                mController.doOauthVerify(LoadingActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA platform) {
                        Toast.makeText(LoadingActivity.this, "授权开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SocializeException e, SHARE_MEDIA platform) {
                        Toast.makeText(LoadingActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete(Bundle value, SHARE_MEDIA platform) {
                        Toast.makeText(LoadingActivity.this, "授权完成", Toast.LENGTH_SHORT).show();
                        //获取相关授权信息
                        mController.getPlatformInfo(LoadingActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMDataListener() {
                            @Override
                            public void onStart() {
                                Toast.makeText(LoadingActivity.this, "获取平台数据开始...", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete(int status, Map<String, Object> info) {
                                if (status == 200 && info != null) {
                                    StringBuilder sb = new StringBuilder();
                                    Set<String> keys = info.keySet();
                                    for (String key : keys) {
                                        sb.append(key + "=" + info.get(key).toString() + "\r\n");
                                    }
                                    Log.d("TestData", sb.toString());

                                    String textuer = (String) info.get("screen_name");
                                    //获取头像
                                    String url = info.get("profile_image_url").toString();
                                    Intent intent = new Intent();
                                    intent.putExtra("profile_image_url", url);
                                    intent.putExtra("screen_name", textuer);
                                    setResult(100, intent);
                                    finish();
                                } else {
                                    Log.d("TestData", "发生错误：" + status);
                                }
                            }
                        });
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        Toast.makeText(LoadingActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
//            case R.id.loading_back_Btn:
//                finish();
//                break;
        }
    }
}
